package fr.oz.mobile.module.profile.nav 
{
	import caurina.transitions.Tweener;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import fr.oz.core.datas.ProfilList;
	import fr.oz.core.events.ProfilEvent;
	import fr.oz.mobile.common.ui.NavSeparator;
	/**
	 * ...
	 * @author Fazermokeur
	 */
	public class ProfilNav extends Sprite
	{
		private var a_item:Vector.<ProfilNavItem>;
		private var _padHandle:Boolean = false;
		
		private var itemSelected:ProfilNavItem;
		
		public function ProfilNav() 
		{
			addEventListener(Event.ADDED_TO_STAGE, init);
			a_item = new Vector.<ProfilNavItem>();
		}
		
		private function init(e:Event):void 
		{
			removeEventListener(Event.ADDED_TO_STAGE, init);
			addEventListener(Event.REMOVED_FROM_STAGE, dispose);
			
			var len:int = ProfilList.profils.length;
			
			var pad:ProfilNavItem;
			for (var i:int = 0; i < len; i++) 
			{
				pad = new ProfilNavItem(ProfilList.profils[i]);
				addChild(pad);
				pad.y = i * 85;
				pad.alpha = 0;
				pad.addEventListener(MouseEvent.CLICK, handleClick);
				pad.addEventListener(ProfilEvent.PROFIL_HANDLE, handleProfilChange);
				pad.addEventListener(ProfilEvent.PROFIL_FREE, handleProfilChange);
				
				var separator:NavSeparator = new NavSeparator();
				addChild(separator);
				separator.alpha = 0;
				separator.y = (i + 1) * 85;
				
				Tweener.addTween([pad, separator], { alpha:1, time:1, delay:i * .2 } );
				
				a_item.push(pad);
			}
		}
		
		private function handleProfilChange(e:ProfilEvent):void 
		{
			if (e.type == ProfilEvent.PROFIL_FREE) {
				_padHandle = false;
			}else {
				_padHandle = true;
			}
		}
		
		private function handleClick(e:MouseEvent):void 
		{
			trace("profil selected : "+(e.currentTarget as ProfilNavItem).profil.name);
			if (!padHandle)
			{
				itemSelected = (e.currentTarget as ProfilNavItem);
				dispatchEvent(new ProfilEvent(ProfilEvent.PROFIL_SELECTED, (e.currentTarget as ProfilNavItem).profil));
			}
		}
		
		public function goSelect():void
		{
			itemSelected.select();
		}
		
		private function dispose(e:Event):void 
		{
			removeEventListener(Event.REMOVED_FROM_STAGE, dispose);
			
		}
		
		public function get padHandle():Boolean { return _padHandle; }
		
	}

}